//
//  ZCPushGuideView.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/28.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCPushGuideView.h"

@interface ZCPushGuideView ()

//@property(strong, nonatomic) ZCPushGuideView *guideView;

@end

@implementation ZCPushGuideView


-(instancetype)init{
    if (self = [super init]) {
        
        [self setupUI];
        
    }
    return self;
}
-(void)setupUI{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    self.frame = window.bounds;
    
    
    UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [topBtn setBackgroundImage:[UIImage imageNamed:@"pushguidetop"] forState:UIControlStateNormal];
    topBtn.size = topBtn.currentBackgroundImage.size;
    topBtn.adjustsImageWhenHighlighted = NO;
    
    UIButton *midBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [midBtn setBackgroundImage:[UIImage imageNamed:@"pushguidemid"] forState:UIControlStateNormal];
    midBtn.size = topBtn.currentBackgroundImage.size;
    midBtn.adjustsImageWhenHighlighted = NO;
    
    UIButton *botBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [botBtn setBackgroundImage:[UIImage imageNamed:@"pushguidebot"] forState:UIControlStateNormal];
    botBtn.size = topBtn.currentBackgroundImage.size;
    botBtn.adjustsImageWhenHighlighted = NO;
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:topBtn];
    [self addSubview:midBtn];
    [self addSubview:botBtn];
    [botBtn addSubview:cancelBtn];
    
    [midBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(midBtn);
        make.bottom.equalTo(midBtn.mas_top).offset(-40);
    }];
    
    [botBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(midBtn);
        make.top.equalTo(midBtn.mas_bottom).offset(5);
    }];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.equalTo(botBtn);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
}

-(void)cancelButtonAction{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

/**
 *  显示推送通知
 */
+(void)show{
    
    NSString *key = @"CFBundleShortVersionString";
    
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    NSString *sandBoxVersion = [[NSUserDefaults standardUserDefaults] stringForKey:key];
    
    if (![currentVersion isEqualToString:sandBoxVersion]) {
    
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        ZCPushGuideView *guideView = [[ZCPushGuideView alloc]init];

        [window addSubview:guideView];
        
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        
    }
    
    
    
}

@end
